package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import dao.FoodSizeDao;
import dao.RowMapper;
import entity.FoodSize;

public class FoodSizeDaoImpl implements FoodSizeDao {
	JDBCTemplate jdbc =new JDBCTemplate();

	
	private class rowMapperImpl implements RowMapper<FoodSize>{
		@Override
		public FoodSize rowMapper(ResultSet rs) throws SQLException {
			FoodSize foodsize = new FoodSize();
			foodsize.setFsId(rs.getInt("fsid"));
			foodsize.setFoodSizeName(rs.getString("foodSizeName"));
			
			return foodsize;
		}
	}

	@Override
	public List<FoodSize> queryAllFoodSize() {
		String sql="SELECT * FROM foodsize";
		return jdbc.queryAll(sql, new rowMapperImpl());
	}

	@Override
	public FoodSize queryFoodSizeByFoodSizeId(int foodSizeId) {
		String sql = "SELECT * FROM foodsize where fsId= ?";
		return jdbc.queryOne(sql, new rowMapperImpl(), foodSizeId);
	}

	@Override
	public List<FoodSize> queryFoodSizeByFoodTypeId(int foodtypeid) {
		String sql ="SELECT DISTINCT foodSizeId,foodSizeName FROM rate "
				+ " INNER JOIN food ON food.`fId`=rate.`foodId`"
				+ " INNER JOIN foodtype ON food.`foodTypeId`=foodType.ftId"
				+ " INNER JOIN foodSize ON rate.`foodSizeId`=foodsize.`fsId`"
				+ " WHERE foodTypeId=?";
		return jdbc.queryAll(sql,new RowMapper<FoodSize>() {

			@Override
			public FoodSize rowMapper(ResultSet rs) throws SQLException {
				return new FoodSize(rs.getInt("foodSizeId"),rs.getString("foodSizeName"));
			}
			
		}, foodtypeid);
	}



}
